let express = require("express")();
let expressStatic = require("express-static")
let mysql = require("mysql");
const { request, response } = require("express");
const port = 8080;

let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '1234567',
    database: 'test',
    timezone: "08:00"
});

sql.connect();

express.get("/getStudentList", (request, response) => {
        sql.query(`SELECT * FROM student`, (error, data) => {
            if (error) {
                console.log(error);
                response.send("error")
            } else {
                response.send(data)
            }
        })
    })
    //删除接口
express.get("/deleteStudent", (request, response) => {
        sql.query(`DELETE  FROM student WHERE id=${request.query.id}`, (error, data) => {
            if (error) {
                console.log(error);
                response.send("error")
            } else {
                response.send("success")
            }
        })
    })
    // 增加学生
express.get("/addStudent", (request, response) => {
        let p = request.query;
        sql.query(`INSERT INTO student (name,age,sex,city,joinDate) VALUES ("${p.name}","${p.age}","${p.sex}","${p.city}","${p.joinDate}")`, (error, data) => {
            if (error) {
                console.log(error)
                response.send("error")
            } else {
                response.send("success")
                console.log(`新增操作成功`)
            }
        })
    })
    //编辑
express.get("/updateStudent", (request, response) => {
        let m = request.query;
        sql.query(`UPDATE student SET name="${m.name}",age="${m.age}",sex="${m.sex}", city="${m.city}",joindate="${m.joinDate}" WHERE id="${m.id}"`, (error, data) => {
            if (error) {
                console.log(error)
                response.send("error")
            } else {
                response.send("success")
                console.log(`修改成功`)
            }
        })
    })
    // sql.query(`SELECT * FROM busers`, (error, data) => {
    //         console.log(data)
    //     })
    //
    //登录
    // 检查用户是否存在 有这个用户就跳转到学生信息页面  
express.get("/login", (request, response) => {
        let n = request.query;
        // console.log(request.query);
        // console.log(data);

        sql.query(`SELECT * FROM busers WHERE username="${n.username}" AND password="${n.password}"`, (error, data) => {
            //sql 语句没错就会进else 里
            if (error) {
                console.log("error")
                response.send("3")
            }
            //输入密码和用户名，但在数据库里找不到，所以长度为0
            if (data.length === 0) {
                response.send("2")
            } else {
                //对象反解析
                response.send(JSON.stringify(data))
            }
            // console.log(request.query.username);
            console.log(data);
        })
    })
    //     // 注册
express.get("/register", (request, response) => {
    let s = request.query;
    // console.log(s);
    sql.query(`INSERT INTO busers (username,password,repassword,usertype) VALUES ("${s.username}","${s.password}","${s.repassword}","${s.usertype}")`, (error, data) => {
        //sql 语句没错就会进else 里
        if (error) {
            console.log(error)
            response.send("error")
        } else {
            response.send("success")
            console.log("新增成功");
        }
        console.log(`${request.query}`)
    })
})
express.use(expressStatic(__dirname + '/static'))

express.listen(port);
console.log(`express is running at ${port}`)